package com.mydomain.Bodega;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.faces.FacesMessages;

@Stateful
@Name("departamentoListar")
@Scope(ScopeType.EVENT)
public class DepartamentoListar implements IDepartamentoListar {

	// All gadget types pulled from the database
	private List<Departamento> mDepartamento;

	// Map to be used for select lists in the UI
	private Map<String, Departamento> mDepartamentoMap;

	// All gadget types pulled from the database
	private List<Subdepartamento> mSubDepartameno;

	@PersistenceContext
	EntityManager em;

	/**
	 * When this bean is created, load up the types and populate the internal
	 * map
	 */
	@Create
	public void loadDepartamento() {
		// Load up the types from the database, ordering them by label
		mDepartamento = em.createQuery("select gt from Departamento gt")
				.getResultList();

		// Convert the list into a map. We use a TreeMap in order
		// to preserve the ordering we asked for in the query
		// above.
		Map<String, Departamento> results = new TreeMap<String, Departamento>();

		for (Departamento us : mDepartamento) {
			results.put(us.getDepDescripcion(), us);
		}

		mDepartamentoMap = results;
	}

	/*
	 * Accessor used in the UI
	 */
	public Map<String, Departamento> getAllDepartamento() {
		return mDepartamentoMap;
	}

	public Collection<SelectItem> getSubDepartamentoSelectItems() {
		Collection<SelectItem> invSub = new ArrayList<SelectItem>();
		Subdepartamento usuario = (Subdepartamento) Contexts
				.getSessionContext().get("usuarioLogeo");
		Integer contar = 0;
		mSubDepartameno = em.createQuery("select s  from Subdepartamento s ")
				.getResultList();
		
		for (Subdepartamento sub : mSubDepartameno) {
			if (sub.getSubdepCodigo() != usuario.getSubdepCodigo())
				invSub.add(new SelectItem(sub.getSubdepCodigo(), sub
						.getDescripcion()));
		}
		return invSub;
	}

	public Collection<SelectItem> getSubDepartamentoTodosSelectItems() {
		Collection<SelectItem> invSub = new ArrayList<SelectItem>();
		mSubDepartameno = em.createQuery("select s  from Subdepartamento s ")
				.getResultList();
		invSub.add(new SelectItem("", ""));
		for (Subdepartamento sub : mSubDepartameno) {
			invSub.add(new SelectItem(sub.getSubdepCodigo(), sub
					.getDescripcion()));
		}
		return invSub;
	}

	/*
	 * Get a converter initialized with the current list of types
	 */
	public Converter getConverter() {
		return new GadgetTypeConverter(mDepartamento);
	}

	static public class GadgetTypeConverter implements Converter, Serializable {
		private List<Departamento> mTypes;

		public GadgetTypeConverter(List<Departamento> types) {
			this.mTypes = types;
		}

		public String getAsString(FacesContext ctx, UIComponent comp, Object obj) {
			if (obj == null)
				return null;

			Departamento type = (Departamento) obj;
			String val = String.valueOf(type.getDepCodigo());

			return val;
		}

		public Object getAsObject(FacesContext ctx, UIComponent comp,
				String strVal) throws ConverterException {
			if (strVal == null || strVal.length() == 0) {
				return null;
			}

			long id = Long.valueOf(strVal).longValue();
			for (Departamento us : mTypes) {
				if (us.getDepCodigo() == id) {
					return us;
				}
			}

			return null;
		}
	}

	@Remove
	@Destroy
	public void destroy() {
	}

}
